import Vue from 'vue'
import VueRouter from 'vue-router'

/* import Login from '../views/login.vue'
// @: 表示 src 的绝对路径, 如果使用它可能会导致最后一级没有提示, 需要格外小心别写错单词了
import Register from '@/views/register.vue'
import Layout from '@/views/layout.vue'
import Detail from '@/views/detail.vue'

import Article from '@/views/article.vue'
import Like from '@/views/like.vue'
import User from '@/views/user.vue'
import Collect from '@/views/collect.vue' */
import { getToken } from '@/utils/localstorage'
import { Toast } from 'vant'
// 安插路由
Vue.use(VueRouter)

// 配置路由
const routes = [
  { path: '/login', component: () => import('@/views/login') },
  { path: '/register', component: () => import('@/views/register') },
  { path: '/detail/:id', component: () => import('@/views/detail') },
  {
    path: '/',
    component: () => import('@/views/layout'),
    redirect: '/article',
    children: [
      { path: 'article', component: () => import('@/views/article') },
      { path: 'like', component: () => import('@/views/like') },
      { path: 'user', component: () => import('@/views/user') },
      { path: 'collect', component: () => import('@/views/collect') }
    ]
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  console.log(to.path, from.path)
  const token = getToken()
  if (token) {
    next()
  } else {
    // 待优化
    const whiteList = ['/login', '/register']
    if (whiteList.includes(to.path)) {
      next()
    } else {
      // 强行跳转至登录页
      next('/login')
      // 提醒用户您还没登录呢!
      Toast('亲, 请登录哦!')
    }
  }
})
// 导出路由对象
export default router
